//
//  MyBarChartDataSet.swift
//  GomroClient
//
//  Created by apple on 2021/4/15.
//  Copyright © 2021 cn.gomro.app. All rights reserved.
//

import UIKit
import Charts

/// 自定义 BarChartDataSet(以实现柱状图红涨绿跌的颜色设置)
class MyBarChartDataSet: BarChartDataSet {

    private var mYValues:[ChartDataEntry]?
    
    override init(entries: [ChartDataEntry]?, label: String?) {
        super.init(entries: entries, label: label)
        self.mYValues = entries
    }
    
    required init() {
        fatalError("init() has not been implemented")
    }
    
    override func color(atIndex index: Int) -> NSUIColor {
        
        if self.mYValues != nil && self.mYValues!.count > index {
            if self.mYValues![index].y >= 0 {
                return colors.first ?? UIColor.red
            }
            else{
                return colors.last ?? UIColor.green
            }
        }
        else{
            return colors.first ?? UIColor.red
        }
    }
}
